package com.happyhome.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import com.happyhome.constants.ComUnitName;
import com.happyhome.constants.RecordStatus;
import com.happyhome.datamodel.Opinion;
import com.happyhome.datamodel.QOpinion;
import com.mysema.query.BooleanBuilder;
import com.mysema.query.jpa.JPQLQuery;
import com.mysema.query.jpa.impl.JPAQuery;
import com.trg.dao.jpa.GenericDAOImpl;

@Repository
public class OpinionDaoImpl extends GenericDAOImpl<Opinion, Long> implements OpinionDao{
	
	@SuppressWarnings("unused")
	private static final Logger LOGGER = LoggerFactory.getLogger(OpinionDaoImpl.class);
	
	@Override
	@PersistenceContext(unitName = ComUnitName.COM_UNIT)
	public void setEntityManager(EntityManager entityManager) {
		super.setEntityManager(entityManager);
	}

	@Override
	public List<Opinion> getAllOpinions() {
		JPQLQuery query = new JPAQuery(em());
		QOpinion opinion = QOpinion.opinion;
		
		query.from(opinion);
		
		BooleanBuilder where = new BooleanBuilder();
		
		where.and(opinion.status.eq(RecordStatus.TRUE));
		
		query.where(where);
		
		List<Opinion> resultOpinions = query.list(opinion);
		return resultOpinions;
	}
	
	@Override
	public List<Opinion> getListOpinionsIsDisplay() {
		JPQLQuery query = new JPAQuery(em());
		QOpinion opinion = QOpinion.opinion;
		
		query.from(opinion);
		
		BooleanBuilder where = new BooleanBuilder();
		
		where.and(opinion.status.eq(RecordStatus.TRUE))
			 .and(opinion.isDisplay.eq(RecordStatus.TRUE));
		
		query.where(where);
		
		List<Opinion> resultOpinions = query.list(opinion);
		return resultOpinions;
	}
	
	@Override
	public List<Opinion> getListOpinionsInListId(List<Long> listId) {
		JPQLQuery query = new JPAQuery(em());
		QOpinion opinion = QOpinion.opinion;
		
		query.from(opinion);
		
		BooleanBuilder where = new BooleanBuilder();
		
		where.and(opinion.status.eq(RecordStatus.TRUE))
			 .and(opinion.id.in(listId));
		
		query.where(where);
		
		List<Opinion> resultRooms = query.list(opinion);
		return resultRooms;
	}

	@Override
	public List<Opinion> getListOpinionsNotInListId(List<Long> listId) {
		JPQLQuery query = new JPAQuery(em());
		QOpinion opinion = QOpinion.opinion;
		
		query.from(opinion);
		
		BooleanBuilder where = new BooleanBuilder();
		
		where.and(opinion.status.eq(RecordStatus.TRUE))
			 .and(opinion.id.notIn(listId));
		
		query.where(where);
		
		List<Opinion> resultRooms = query.list(opinion);
		return resultRooms;
	}

	@Override
	public List<Opinion> getListOpinionIsDisplay() {
		JPQLQuery query = new JPAQuery(em());
		QOpinion opinion = QOpinion.opinion;
		
		query.from(opinion);
		
		BooleanBuilder where = new BooleanBuilder();
		
		where.and(opinion.status.eq(RecordStatus.TRUE))
			 .and(opinion.isDisplay.eq(RecordStatus.TRUE));
		
		query.where(where);
		
		List<Opinion> resultRooms = query.list(opinion);
		return resultRooms;
	}
	
	@Override
	public List<Opinion> getListOpinionNewest(Long limit) {
		JPQLQuery query = new JPAQuery(em());
		QOpinion opinion = QOpinion.opinion;
		
		query.from(opinion);
		
		BooleanBuilder where = new BooleanBuilder();
		
		where.and(opinion.status.eq(RecordStatus.TRUE));
		
		query.where(where).orderBy(opinion.dateCreated.desc()).limit(limit);
		
		List<Opinion> resultRooms = query.list(opinion);
		return resultRooms;
	}
}
